﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ApiPrototype.Models;

namespace ApiPrototype.Controllers
{ 
    public class TestController : Controller
    {
        private iphlEntities db = new iphlEntities();

        //
        // GET: /Test/

        public ViewResult Index()
        {
            try
            {
                var bb_mededeling = db.BB_MEDEDELING.Include(b => b.OLOD);
                return View(bb_mededeling.ToList());
            }
            catch (Exception ex)
            {
                var med = new BB_MEDEDELING { MED_ID = 1, MED_ONDERWERP = ex.ToString() };
                var list = new List<BB_MEDEDELING> { med };
                return View(list);
            }
        }

        //
        // GET: /Test/Details/5

        public ViewResult Details(int id)
        {
            BB_MEDEDELING bb_mededeling = db.BB_MEDEDELING.Find(id);
            return View(bb_mededeling);
        }

        //
        // GET: /Test/Create

        public ActionResult Create()
        {
            ViewBag.OLO_ID = new SelectList(db.OLOD, "OLO_ID", "OLO_OMSCHRIJVING");
            return View();
        } 

        //
        // POST: /Test/Create

        [HttpPost]
        public ActionResult Create(BB_MEDEDELING bb_mededeling)
        {
            if (ModelState.IsValid)
            {
                db.BB_MEDEDELING.Add(bb_mededeling);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.OLO_ID = new SelectList(db.OLOD, "OLO_ID", "OLO_OMSCHRIJVING", bb_mededeling.OLO_ID);
            return View(bb_mededeling);
        }
        
        //
        // GET: /Test/Edit/5
 
        public ActionResult Edit(int id)
        {
            BB_MEDEDELING bb_mededeling = db.BB_MEDEDELING.Find(id);
            ViewBag.OLO_ID = new SelectList(db.OLOD, "OLO_ID", "OLO_OMSCHRIJVING", bb_mededeling.OLO_ID);
            return View(bb_mededeling);
        }

        //
        // POST: /Test/Edit/5

        [HttpPost]
        public ActionResult Edit(BB_MEDEDELING bb_mededeling)
        {
            if (ModelState.IsValid)
            {
                db.Entry(bb_mededeling).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.OLO_ID = new SelectList(db.OLOD, "OLO_ID", "OLO_OMSCHRIJVING", bb_mededeling.OLO_ID);
            return View(bb_mededeling);
        }

        //
        // GET: /Test/Delete/5
 
        public ActionResult Delete(int id)
        {
            BB_MEDEDELING bb_mededeling = db.BB_MEDEDELING.Find(id);
            return View(bb_mededeling);
        }

        //
        // POST: /Test/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            BB_MEDEDELING bb_mededeling = db.BB_MEDEDELING.Find(id);
            db.BB_MEDEDELING.Remove(bb_mededeling);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}